mercurial.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  4. import logging
  5. import os
  6. from pip._vendor.six.moves import configparser
  7. from pip._internal.exceptions import BadCommand, SubProcessError
  8. from pip._internal.utils.misc import display_path
  9. from pip._internal.utils.subprocess import make_command
  10. from pip._internal.utils.temp_dir import TempDirectory
  11. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  12. from pip._internal.utils.urls import path_to_url
  13. from pip._internal.vcs.versioncontrol import (
  14. VersionControl,
  15. find_path_to_setup_from_repo_root,
  16. vcs,
  17. )
  18. if MYPY_CHECK_RUNNING:
  19. from pip._internal.utils.misc import HiddenText
  20. from pip._internal.vcs.versioncontrol import RevOptions
  21. logger = logging.getLogger(__name__)
  22. class Mercurial(VersionControl):
  23. name = 'hg'
  24. dirname = '.hg'
  25. repo_name = 'clone'
  26. schemes = (
  27. 'hg', 'hg+file', 'hg+http', 'hg+https', 'hg+ssh', 'hg+static-http',
  28. )
  29. @staticmethod
  30. def get_base_rev_args(rev):
  31. return [rev]
  32. def export(self, location, url):
  33. # type: (str, HiddenText) -> None
  34. """Export the Hg repository at the url to the destination location"""
  35. with TempDirectory(kind="export") as temp_dir:
  36. self.unpack(temp_dir.path, url=url)
  37. self.run_command(
  38. ['archive', location], cwd=temp_dir.path
  39. )
  40. def fetch_new(self, dest, url, rev_options):
  41. # type: (str, HiddenText, RevOptions) -> None
  42. rev_display = rev_options.to_display()
  43. logger.info(
  44. 'Cloning hg %s%s to %s',
  45. url,
  46. rev_display,
  47. display_path(dest),
  48. )
  49. self.run_command(make_command('clone', '--noupdate', '-q', url, dest))
  50. self.run_command(
  51. make_command('update', '-q', rev_options.to_args()),
  52. cwd=dest,
  53. )
  54. def switch(self, dest, url, rev_options):
  55. # type: (str, HiddenText, RevOptions) -> None
  56. repo_config = os.path.join(dest, self.dirname, 'hgrc')
  57. config = configparser.RawConfigParser()
  58. try:
  59. config.read(repo_config)
  60. config.set('paths', 'default', url.secret)
  61. with open(repo_config, 'w') as config_file:
  62. config.write(config_file)
  63. except (OSError, configparser.NoSectionError) as exc:
  64. logger.warning(
  65. 'Could not switch Mercurial repository to %s: %s', url, exc,
  66. )
  67. else:
  68. cmd_args = make_command('update', '-q', rev_options.to_args())
  69. self.run_command(cmd_args, cwd=dest)
  70. def update(self, dest, url, rev_options):
  71. # type: (str, HiddenText, RevOptions) -> None
  72. self.run_command(['pull', '-q'], cwd=dest)
  73. cmd_args = make_command('update', '-q', rev_options.to_args())
  74. self.run_command(cmd_args, cwd=dest)
  75. @classmethod
  76. def get_remote_url(cls, location):
  77. url = cls.run_command(
  78. ['showconfig', 'paths.default'],
  79. cwd=location).strip()
  80. if cls._is_local_repository(url):
  81. url = path_to_url(url)
  82. return url.strip()
  83. @classmethod
  84. def get_revision(cls, location):
  85. """
  86. Return the repository-local changeset revision number, as an integer.
  87. """
  88. current_revision = cls.run_command(
  89. ['parents', '--template={rev}'], cwd=location).strip()
  90. return current_revision
  91. @classmethod
  92. def get_requirement_revision(cls, location):
  93. """
  94. Return the changeset identification hash, as a 40-character
  95. hexadecimal string
  96. """
  97. current_rev_hash = cls.run_command(
  98. ['parents', '--template={node}'],
  99. cwd=location).strip()
  100. return current_rev_hash
  101. @classmethod
  102. def is_commit_id_equal(cls, dest, name):
  103. """Always assume the versions don't match"""
  104. return False
  105. @classmethod
  106. def get_subdirectory(cls, location):
  107. """
  108. Return the path to setup.py, relative to the repo root.
  109. Return None if setup.py is in the repo root.
  110. """
  111. # find the repo root
  112. repo_root = cls.run_command(
  113. ['root'], cwd=location).strip()
  114. if not os.path.isabs(repo_root):
  115. repo_root = os.path.abspath(os.path.join(location, repo_root))
  116. return find_path_to_setup_from_repo_root(location, repo_root)
  117. @classmethod
  118. def get_repository_root(cls, location):
  119. loc = super(Mercurial, cls).get_repository_root(location)
  120. if loc:
  121. return loc
  122. try:
  123. r = cls.run_command(
  124. ['root'],
  125. cwd=location,
  126. log_failed_cmd=False,
  127. )
  128. except BadCommand:
  129. logger.debug("could not determine if %s is under hg control "
  130. "because hg is not available", location)
  131. return None
  132. except SubProcessError:
  133. return None
  134. return os.path.normpath(r.rstrip('\r\n'))
  135. vcs.register(Mercurial)